package com.software.chemistry.service.Impl;

import com.software.chemistry.entity.StudentInfo;
import com.software.chemistry.entity.UserQuery;
import com.software.chemistry.repository.StudentInfoRepository;
import com.software.chemistry.service.StudentInfoService;
import com.software.chemistry.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class StudentInfoServiceImpl implements StudentInfoService {
    @Autowired
    private StudentInfoRepository studentInfoRepository ;
    @Override
    public StudentInfo findStudentInfoByUserId(String userId) {
        StudentInfo find = studentInfoRepository.findStudentInfoByUserId(userId) ;
        return find;
    }

    @Override
    public StudentInfo createStudentInfo(StudentInfo studentInfo) {
        String md5data= SecurityUtil.md5String(studentInfo.getPassword());
        String md5Password=SecurityUtil.upperCaseMd5(md5data);
        studentInfo.setPassword(md5Password);
        StudentInfo save = studentInfoRepository.save(studentInfo);
        return save;
    }

    @Override
    public List<StudentInfo> findStudentInfoByUserName(String param_username) {
        return studentInfoRepository.findStudentInfoByUserName(param_username);
    }

    @Override
    public List<StudentInfo> verifyLoginUser(String userName, String password) {
        String md5data=SecurityUtil.md5String(password);
        String md5Password=SecurityUtil.upperCaseMd5(md5data);
        List<StudentInfo> studentInfo=studentInfoRepository.findStudentByUserNameAndPassword(userName,md5Password);
        return studentInfo;
    }

    @Override
    public List<UserQuery> findStudentInfosByUserIdOrUserName(String str, String str1) {
        List<StudentInfo> studentInfoList = studentInfoRepository.findStudentInfosByUserIdOrUserName(str, str1) ;
        List<UserQuery> userQueries = new ArrayList<>() ;
        for(int i = 0; i < studentInfoList.size(); i ++ ){
            UserQuery userQuery = new UserQuery() ;
            userQuery.setUserName(studentInfoList.get(i).getUserName());
            userQuery.setUserId(studentInfoList.get(i).getUserId());
            userQuery.setIcon(studentInfoList.get(i).getIcon());
            userQueries.add(userQuery) ;
        }
        return userQueries ;
    }

    @Override
    public List<UserQuery> findStudentInfos() {
        List<StudentInfo> studentInfoList = studentInfoRepository.findAll();
        List<UserQuery> userQueries = new ArrayList<>() ;
        for (int i = 0; i < studentInfoList.size(); i ++ ){
            UserQuery userQuery = new UserQuery();
            userQuery.setIcon(studentInfoList.get(i).getIcon());
            userQuery.setUserName(studentInfoList.get(i).getUserName());
            userQuery.setUserId(studentInfoList.get(i).getUserId());
            userQueries.add(userQuery);
        }
        return userQueries ;
    }

    @Override
    public List<UserQuery> findStudentInfosByClassId(String classId) {
        List<StudentInfo> studentInfoList = studentInfoRepository.findStudentInfosByClassId(classId);
        System.out.println("findStudentInfosByClassId: " + "size: " + studentInfoList.size());
        //System.out.println("findStudentInfosByClassId: " + studentInfoList.get(0));
        List<UserQuery> userQueries = new ArrayList<>() ;
        for (int i = 0; i < studentInfoList.size(); i ++ ){
            UserQuery userQuery = new UserQuery();
            System.out.println(studentInfoList.get(i).getUserName());
            userQuery.setIcon(studentInfoList.get(i).getIcon());
            userQuery.setUserName(studentInfoList.get(i).getUserName());
            userQuery.setUserId(studentInfoList.get(i).getUserId());
            userQueries.add(userQuery);
        }
        return userQueries ;
    }

    @Override
    public StudentInfo updateStudentInfo(StudentInfo studentInfo) {
        StudentInfo save = studentInfoRepository.save(studentInfo);
        return save;
    }
}
